<script type="text/javascript">
    var dialogOpen, form, table;
    layui.use(['table'], function () {
        table = layui.table;
        layAjaxTable(table, {
            url: "/crud/handler",
            where: {flag: -1,"code":"${.list.Bind}"},
            cols: [[
                {title: '序号', width: 70, templet: '#indexTpl'},
                ${- range .list.Column}
                {field: '${.Field}', title: '${.Title}', width: 100,type: '${.Type}',templet: '${.Templet}'},
                ${- end}
                {title: '操作', toolbar: '#barDemo', width: 140}
            ]],
            page:${.list.Page}
        });
        table.on('tool(demo)', function (obj) {
            var data = obj.data;

            if (obj.event === 'del') {
                layer.confirm('确认删除该数据？', {title: "系统提示"}, function (index) {
                    ajaxDel(obj, index);
                });
            }
            if (obj.event === 'edit') {
                juicer.renderTo("updatetpl", data, "update-form");
                form.render(null, "updateForm");
                editLayer();
            }
        });
    });
    function ajaxDel(obj, index) {

        var code = ""
        ${- range .list.Toolbar}
            ${- if eq "del" .Operate}
                code = ${.Bind}
            ${- end}
        ${- end}
        if(code == ""){
            layer.msg("删除发生异常");
            return
        }
        var data = {
            "code":code,
            ${- range $idx,$el:=.list.Key}
                ${.}:obj.data[${.}]
                ${- if ne $idx (len $.list.Search)}
                ,
                ${- end}
            ${- end}
        }

        var url = "/crud/handler";
        postAjax(url, data, function (res) {
            if (res["code"] == 0) {
                layer.msg(res["msg"]);
                obj.del();
                layer.close(index);
            } else {
                layer.msg(res["msg"]);
            }

        }, function () {
            layer.msg("删除发生异常");
        });
    }
    //添加
    function addDialog() {
        var refresh = true;
        var href = "/crud/ui?view=company.add";
        dialogOpen=layOpenUrl({
            title: "添加公司",
            area:['600px', "400px"],
            content: href,
            cancel: function () {
                refresh = false;
            },
            end: function () {
                refresh && query();
            }
        });
    }

    function editLayer() {
        var dom = $("#update-form");
        dialogOpen = layer.open({
            title: '编辑公司'
            , type: 1
            , area:['600px', "400px"]
            , content: dom
            , btn: ['确定', '取消']
            , yes: function () {
                $("#update-submit").click();
            }
        });
    }

    function query() {
        table.reload('list-table', {
            where:{
                ${- range $idx,$el:= .list.Search}
                    ${- if eq "input" $el.Dom}
                        ${$el.EnName}: $.trim($("#${$el.Id}").val())
                        ${- if ne $idx (len $.list.Search)}
                        ,
                        ${- end}
                    ${- end}
                    ${- if eq "select" $el.Dom}
                        ${$el.EnName}: $("#${$el.Id} option:selected").val()
                        ${- if ne $idx (len $.list.Search)}
                        ,
                        ${- end}
                    ${- end}
                ${- end}
            }
        });
    }

    $(function () {
        $("#add-btn").click(addDialog);
        $("#query-btn").click(query);
        regEnterQuery(query)
    });
</script>
